package com.ruoyi.project.cms.menu.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.dept.domain.MDept;
import com.ruoyi.project.cms.menu.domain.SysMenu;
import com.ruoyi.project.cms.menu.service.SysMenuService;
import com.ruoyi.project.cms.role.domain.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公司:
 * 作者:胖虎
 * 类名:SysMenuController
 * 描述:
 * 时间:2020-05-19
 */

@Controller
@RequestMapping("/cms/menu")
public class SysMenuController extends BaseController {

    private String prefix = "cms/menu";

    @Autowired
    private SysMenuService menuService;


    /**
     * 加载相应角色菜单列表树
     */
    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    public List<Ztree> roleMenuTreeData(SysRole role) {
        List<Ztree> ztrees = menuService.roleMenuTreeData(role);
        System.out.println(ztrees);
        return ztrees;
    }

    /**
     * 权限页面的跳转
     *
     * @return
     */
    @GetMapping()
    public String menu() {
        return prefix + "/menu";
    }


    /**
     * 所有菜单的查询
     *
     * @param menu
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public List<SysMenu> list(SysMenu menu) {

        List<SysMenu> list = menuService.selectMenuList(menu);

        return list;
    }

    /**
     * 新增菜单的跳转
     * @return
     */
    /**
     * 新增
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<SysMenu> list = menuService.selectMenuList(new SysMenu());
        mmap.put("menu", list);
        return prefix + "/add";
    }

    /**
     * 添加菜单
     *
     * @param menu
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysMenu menu) {
        if (UserConstants.MENU_NAME_NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) {
            return error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        return toAjax(menuService.insertMenu(menu));
    }

    /**
     * 修改菜单的跳转带过去数据
     *
     * @param deptId
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{menuId}")
    public String edit(@PathVariable("menuId") Long menuId, ModelMap mmap) {
        SysMenu menu = menuService.selectMenuById(menuId);
        //根据父id查询父菜单的name
        String   pmenuName="我没有父菜单";
        if(menu.getParentId()!=0){
             pmenuName = menuService.selectParentNameByPid(menu.getParentId());
        }

        System.out.println("我是:"+pmenuName);
        menu.setPmenuName(pmenuName);
        if (menu.getMenuType().equals("M")) {
            menu.setMenuType("目录");
        } else if (menu.getMenuType().equals("F")) {
            menu.setMenuType("按钮");
        } else if (menu.getMenuType().equals("C")) {
            menu.setMenuType("菜单");
        }

        mmap.put("menu", menu);
        return prefix + "/edit";
    }


    @PostMapping("/update")
    @ResponseBody
    public AjaxResult update(SysMenu menu) {
      System.out.println("我是谁"+menu);


        return toAjax(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单
     * @param deptId
     * @return
     */
    @GetMapping("/remove/{menuId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("menuId") Long menuId)
    {

        return   toAjax(menuService.removeMenu(menuId));

    }

}
